<?php
// 读取某个目录下的所有文件和文件夹
// 然后展示出来

// 包含启动脚本
require __DIR__ . '/../bootstrap.php';

// 判断是否登录
if (!is_login()) {
    show_msg_exit('请等录', './login.php');
}

// 默认目录
$dir = __DIR__;

// 如果传递了，就替换掉默认的
if (!empty($_GET['dir']) && is_dir($_GET['dir'])) {
    $dir = $_GET['dir'];
}

// 目录要有读权限
if (!is_readable($dir)) {
    trigger_error("没有读权限：`$dir`", E_USER_ERROR);
}

// 读取目录
$h = opendir($dir);

// 循环读取
$dirData = [];
$fileData = [];
while (false !== $filename = readdir($h)) {
    if (is_dir("$dir/$filename")) {
        $dirData[] = $filename;
    } else {
        $fileData[] = $filename;
    }
}

?><!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>文件列表</title>
</head>
<body>
    <?php require __DIR__ . '/../logout.html' ?>

    <div>
        <p><?= $dir ?></p>
    </div>

    <div>
        <form action="./create-dir.php" method="POST">
            <input type="hidden" name="dir" value="<?= $dir ?>">
            <input type="text" name="name">
            <input type="submit" value="新建目录">
        </form>

        <form action="./create-file.php" method="POST">
            <input type="hidden" name="dir" value="<?= $dir ?>">
            <input type="text" name="name">
            <input type="submit" value="新建文件">
        </form>
    </div>

    <table>
        <?php foreach ($dirData as $value): ?>
            <tr>
                <td>
                    <img src="./images/dir.jpg" style="width:20px;height:20px;" alt="图标">
                </td>

                <td>
                    <a href="./read-dir.php?dir=<?= realpath("$dir/$value") ?>"><?= $value ?></a>
                </td>

                <td>
                    <?php if ('.' != $value && '..' != $value): ?>
                        <button class="rename" data-oldname="<?= realpath("$dir/$value") ?>">重命名</button>
                        <form action="./rename.php" method="POST" style="display: none">
                            <input type="hidden" name="oldname" value="<?= realpath("$dir/$value") ?>">
                            <input type="hidden" name="newname" value="">
                        </form>

                        <button class="delete">删除</button>
                        <form action="./delete.php" method="POST" style="display: none">
                            <input type="hidden" name="file" value="<?= realpath("$dir/$value") ?>">
                        </form>
                    <?php endif ?>
                </td>
            </tr>
        <?php endforeach ?>

        <?php foreach ($fileData as $value): ?>
            <tr>
                <td>
                    <img src="./images/file.jpg" style="width:20px;height:20px;" alt="图标">
                </td>

                <td>
                    <a href="./read-file.php?file=<?= realpath("$dir/$value") ?>"><?= $value ?></a>
                </td>

                <td>
                    <button class="rename" data-oldname="<?= realpath("$dir/$value") ?>">重命名</button>
                    <form action="./rename.php" method="POST" style="display: none">
                        <input type="hidden" name="oldname" value="<?= realpath("$dir/$value") ?>">
                        <input type="hidden" name="newname" value="">
                    </form>

                    <button class="delete">删除</button>
                    <form action="./delete.php" method="POST" style="display: none">
                        <input type="hidden" name="file" value="<?= realpath("$dir/$value") ?>">
                    </form>
                </td>
            </tr>
        <?php endforeach ?>
    </table>

    <script>
        document.querySelectorAll('.delete').forEach((item) => {
            item.addEventListener('click', (e) => {
                if (confirm('你要删除吗？')) {
                    item.nextElementSibling.submit();
                }
            });
        });

        document.querySelectorAll('.rename').forEach((item) => {
            item.addEventListener('click', (e) => {
                var newname = prompt('重命名', item.dataset.oldname);

                if (newname !== false) {
                    item.nextElementSibling[1].value = newname;
                    item.nextElementSibling.submit();
                }
            });
        });
    </script>
</body>
</html>